package zxn.circleboard.demo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.BoringLayout;
import android.text.Layout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zxn on 2020/3/10.
 */
public class CircleBoardView
        extends View {

    private static final String TAG = "CircleBoardView";
    /**
     * 画笔颜色
     */
    private int mExampleColor = Color.parseColor("#00bb9c");

    private float mExampleDimension = 0; // TODO: use a default from R.dimen...
    private Drawable mExampleDrawable;
    private TextPaint mTextPaint;
    private float mTextWidth;
    private float mTextHeight;
    /**
     * 四周边距
     */
    private int mPadding;
    /**
     * 小圆环画笔
     */
    private Paint mRingPaint;

    /**
     * 绘制外圆圈的画笔.
     */
    private Paint mOuterCirclePaint;


    //    /**
//     * 小圆圈内圆半径
//     */
//    private int mInnerCircleRadius;
    private Context context;
    /**
     * 小圆圈圆环宽度
     */
    private int mRingWidth;
    /**
     * outer
     * 小圆圈外部圆半径
     */
    private int mOuterRadius;
    /**
     * 绘制圆环的列数
     */
    private int mColumnNum = 12;
    /**
     * 绘制圆环的行数
     */
    private int mRowNum = 65;
    /**
     * 两个环之间的圆心距离.
     */
    private int mCenterDistance;
    /**
     * 两个圆环之间的间隙.
     */
    private double mSpace;


    /**
     * 圆环坐标信息
     */
    private List<PointInfo> mRingInfoList = new ArrayList<>();
    /**
     * 外圆圈的坐标信息.
     */
    private List<PointInfo> mOuterList = new ArrayList<>();
    /**
     * 绘制模式0,1
     */
    private int mMode;

    private int mViewHeight;

    public CircleBoardView(Context context) {
        super(context);
        this.context = context;
        init(null, 0);
    }

    public CircleBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs, 0);
    }

    public CircleBoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init(attrs, defStyle);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.CircleBoardView, defStyle, 0);

        mExampleColor = a.getColor(
                R.styleable.CircleBoardView_exampleColor,
                mExampleColor);
        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
        // values that should fall on pixel boundaries.
        mExampleDimension = a.getDimension(
                R.styleable.CircleBoardView_exampleDimension,
                mExampleDimension);

        if (a.hasValue(R.styleable.CircleBoardView_exampleDrawable)) {
            mExampleDrawable = a.getDrawable(
                    R.styleable.CircleBoardView_exampleDrawable);
            mExampleDrawable.setCallback(this);
        }

        a.recycle();

        mPadding = dip2px(context, 10);
//        mInnerCircleRadius = dip2px(context, 8);
        mOuterRadius = dip2px(context, 12);//8
        mRingWidth = dip2px(context, 2);


        this.mRingPaint = new Paint();
        this.mRingPaint.setAntiAlias(true); //消除锯齿
        this.mRingPaint.setStyle(Paint.Style.STROKE); //绘制空心圆
        this.mRingPaint.setColor(Color.parseColor("#00bb9c"));
        this.mRingPaint.setStrokeWidth(mRingWidth);

        mOuterCirclePaint = new Paint();
        mOuterCirclePaint.setAntiAlias(true);
        mOuterCirclePaint.setStyle(Paint.Style.FILL);
        mOuterCirclePaint.setColor(mExampleColor);

        //this.mRingPaint.setStrokeWidth(2);
        //        // Set up a default TextPaint object
//        mTextPaint = new TextPaint();
//        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
//        mTextPaint.setTextAlign(Paint.Align.LEFT);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                event.getX();
//                event.getY();
//                break;
            case MotionEvent.ACTION_MOVE:
                onTouchMove(event.getX(), event.getY());
                invalidate();
                break;
//            case MotionEvent.ACTION_UP:
//                break;
//            case MotionEvent.ACTION_CANCEL:
//                break;
//            default:
//                break;
        }
        return true;
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mSpace = (getWidth() - getPaddingLeft() - getPaddingRight() - mOuterRadius * 2 * mColumnNum) / 15d;
        mViewHeight = mOuterRadius * mRowNum + (int) Math.ceil(mSpace * (mRowNum - 1));
        mCenterDistance = (int) (mOuterRadius * 2 + mSpace);
        int radius = mOuterRadius - mRingWidth / 2;
        for (int i = 0; i < mColumnNum; i++) {
            float cx = getPaddingLeft() + mOuterRadius + mCenterDistance * i;
            for (int j = 0; j < mRowNum; j++) {
                float cy = getPaddingTop() + mOuterRadius + mCenterDistance * j;
                Log.i(TAG, "drawRing: cx:" + cx + "---cy:" + cy);
                //canvas.drawCircle(cx, cy, mOuterRadius - mRingWidth / 2, this.mRingPaint);
                mRingInfoList.add(new PointInfo(cx, cy, radius, mOuterRadius));
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);


        int width = widthSize;
        int height;

        mSpace = (width - getPaddingLeft() - getPaddingRight() - mOuterRadius * 2 * mColumnNum) / 15d;
        //int vSpace = mSpace <=0 ? 10 : mSpace;
        int vSpace = (int) Math.ceil(mSpace);
        mViewHeight = mOuterRadius * 2 * mRowNum + (vSpace * (mRowNum - 1)) + getPaddingBottom();
        setMeasuredDimension(width, mViewHeight);
    }

    private int measureHeight(int measureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = mViewHeight;
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawRing(canvas);
        drawOuterCircle(canvas);

        //canvasDrawer.onDraw(canvas);
        //pathDrawer.onDraw(canvas, currentDrawingPath, paths);
    }

    /**
     * 绘制小圆环
     *
     * @param canvas
     */
    private void drawRing(Canvas canvas) {
        /*for (int i = 0; i < mColumnNum; i++) {
            float cx = getPaddingLeft() + mOuterRadius + mCenterDistance * i;
            for (int j = 0; j < mRowNum; j++) {
                float cy = getPaddingTop() + mOuterRadius + mCenterDistance * j;
                Log.i(TAG, "drawRing: cx:" + cx + "---cy:" + cy);
                canvas.drawCircle(cx, cy, mOuterRadius - mRingWidth / 2, this.mRingPaint);
            }
        }*/
        for (PointInfo pointInfo : mRingInfoList) {
            canvas.drawCircle(pointInfo.cx, pointInfo.cy, pointInfo.ringRadius, this.mRingPaint);
        }
    }

    /**
     * 绘制外圆圈.
     *
     * @param canvas
     */
    private void drawOuterCircle(Canvas canvas) {
        //drawCircle(float cx, float cy, float radius, @NonNull Paint paint)
//        float cx = 200;
//        float cy = 200;
//        canvas.drawCircle(cx, cy, mOuterRadius, this.mOuterCirclePaint);

        for (PointInfo pointInfo : mOuterList) {
            canvas.drawCircle(pointInfo.cx, pointInfo.cy, pointInfo.outerRadius, this.mOuterCirclePaint);
        }
    }

    private void onTouchMove(float eventX, double eventY) {
        Log.i(TAG, "onTouchMove: eventX:" + eventX + "--eventY:" + eventY);
        for (PointInfo pointInfo : mRingInfoList) {
            if (Math.abs(eventX - pointInfo.cx) <= mOuterRadius
                    && Math.abs(eventY - pointInfo.cy) <= mOuterRadius) {
                if (mMode == 0) {
                    if (!mOuterList.contains(pointInfo)) {
                        mOuterList.add(pointInfo);
                    }
                } else {
                    if (mOuterList.contains(pointInfo)) {
                        mOuterList.remove(pointInfo);
                    }
                }
            }
        }
    }

    public void reset() {
        mOuterList.clear();
        invalidate();
    }

    /**
     * @param mode 0绘制,1擦除,
     */
    public void setDrawMode(int mode) {
        mMode = mode;
    }


   /* private int getDesiredHeight() {
        return Math.max(
                getDesiredHeight(mLayout, true),
                getDesiredHeight(mHintLayout, mEllipsize != null));
    }*/
}
